package com.zh.note.huawei.onehundred.流水线;

import java.util.*;

/**
 * @Author: zhanghuan
 * @date: 2023/9/3 20:52
 * @description: create it
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // String[] strs = sc.nextLine().split(" ");
        // String[] jobs = sc.nextLine().split(" ");
        String[] strs = {"3", "5"};
        String[] jobs = {"8", "4", "3", "2", "10"};
        solution(strs, jobs);
    }

    private static void solution(String[] strs, String[] jobs) {
        // 流水线个数
        int m = Integer.parseInt(strs[0]);
        //作业数量
        int n = Integer.parseInt(strs[1]);
        int[] array = new int[n];

        for (int j = 0; j < n; j++) {
            array[j] = Integer.parseInt(jobs[j]);
        }
        //从小到大排序
        Arrays.sort(array);
        List<Task> list = new ArrayList<>();
        for (int i = 0; i < array.length; i++) {
            if (list.size() < m) {
                list.add(new Task(array[i], array[i]));
            } else {
                Collections.sort(list);
                Task medic = list.get(0);
                medic.sum += array[i];
            }
        }
        Collections.sort(list);
        //最后一个元素就是总时长
        System.out.println(list.get(list.size() - 1).sum);
    }

    static class Task implements Comparable {
        public int end;
        public int sum;

        public Task(int end, int sum) {
            this.end = end;
            this.sum = sum;
        }


        @Override
        public int compareTo(Object o) {
            Task medic = (Task) o;
            //从小到大排序
            return this.sum - medic.sum;

        }
    }
}
